//
//  EmoticonInputView.swift
//  VVbo
//
//  Created by Mr.Run on 2017/9/11.
//  Copyright © 2017年 Mr.Run. All rights reserved.
//

import UIKit

let cellID = "cellID"

class EmoticonInputView: UIView {

    @IBOutlet weak var pageControl: UIPageControl!
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    @IBOutlet weak var toolbar: EmoticonToolbar!
    
    fileprivate var selectedEmoticonCallBack:((_ emotion:Emoticon?)->())?
    
    class func inputView(selectedEmoticon:@escaping (_ emotion:Emoticon?)->()) -> EmoticonInputView{
        let nib = UINib(nibName: "EmoticonInputView", bundle: nil)
        
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! EmoticonInputView
        v.selectedEmoticonCallBack = selectedEmoticon
        return v
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        collectionView.backgroundColor = UIColor.white
        toolbar.delegate = self
//        let nib = UINib(nibName: "EmoticonCell", bundle: nil)
        
//        collectionView.register(nib, forCellWithReuseIdentifier: cellID)
        collectionView.register(EmoticonCell.self, forCellWithReuseIdentifier: cellID)
        
        let bundle = EmoticonManager.shared.bundle
        guard let normalImage = UIImage(named: "compose_keyboard_dot_normal", in: bundle, compatibleWith: nil),
            let selectedImage = UIImage(named: "compose_keyboard_dot_selected", in: bundle, compatibleWith: nil) else{
                return
        }
//        pageControl.currentPageIndicatorTintColor = UIColor(patternImage: selectedImage)
//        pageControl.pageIndicatorTintColor = UIColor(patternImage: normalImage)
        
        pageControl.setValue(selectedImage, forKey: "_currentPageImage")
        pageControl.setValue(normalImage, forKey: "_pageImage")
        
    }
}

extension EmoticonInputView: EmoticonToolbarDelegate {
    
    func emoticonToolbarDidSelectedItemIndex(toolbar: EmoticonToolbar, index: Int) {
        let indexPath = IndexPath(item: 0, section: index)
        collectionView.scrollToItem(at: indexPath, at: .left, animated: true)
        toolbar.selectedIndex = index
    }
    
}


extension EmoticonInputView: UICollectionViewDataSource{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return EmoticonManager.shared.packages.count
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return EmoticonManager.shared.packages[section].numberOfPages
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! EmoticonCell
        
        cell.emoticons = EmoticonManager.shared.packages[indexPath.section].emoticon(page: indexPath.item)
//        cell.label.text = "\(indexPath)"
        cell.delegate = self
        return cell
    }
    
}

extension EmoticonInputView: UICollectionViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        var center = scrollView.center
        center.x += scrollView.contentOffset.x
        let paths = collectionView.indexPathsForVisibleItems
        
        var targetIndexPath:IndexPath?
        
        for indexPath in paths {
            
            let cell = collectionView.cellForItem(at: indexPath)
            
            if cell?.frame.contains(center) == true {
                targetIndexPath = indexPath
                break
            }
        }
        
        guard let target = targetIndexPath else {
            return
        }
        
        toolbar.selectedIndex = target.section
        
        pageControl.numberOfPages = collectionView.numberOfItems(inSection: target.section)
        pageControl.currentPage = target.item
        
    }
    
}

extension EmoticonInputView: EmoticonCellDelegate {

    func emoticonCellDidSelectedEmoticon(cell: EmoticonCell, em: Emoticon?) {
        selectedEmoticonCallBack!(em)
        guard let em = em else{
            return
        }
        
        EmoticonManager.shared.recentEmoticon(em: em)
        
        let indexPath = collectionView.indexPathsForVisibleItems[0]
        
        if indexPath.section == 0{
            return
        }
        
        var indexSet = IndexSet()
        indexSet.insert(0)
        collectionView.reloadSections(indexSet)
    }

}

